package com.industrial.operation.service.impl;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.security.utils.DictUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.CityOptions;
import com.industrial.operation.domain.OperationArea;
import com.industrial.operation.domain.OperationJd;
import com.industrial.operation.domain.gxhzb.ChartsDbData;
import com.industrial.operation.mapper.OperationJdFxMapper;
import com.industrial.operation.service.IOperationJdFxService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OperationJdFxServiceImpl implements IOperationJdFxService {

    @Autowired
    private OperationJdFxMapper operationJdFxMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final Integer BTACH_COUNT = 300;


    /**
     * 查询列表
     *
     * @param operationJd
     * @return
     */
    @Override
    public List<OperationJd> list(OperationJd operationJd) {

        List<OperationJd> listResult = operationJdFxMapper.list(operationJd);

        return listResult;
    }

    /**
     * 导入数据
     *
     * @param multipartFile
     * @param operationJd
     */
    @Override
    public void importData(MultipartFile multipartFile, OperationJd operationJd) {

        try{
            ExcelUtil<OperationJd> util = new ExcelUtil<OperationJd>(OperationJd.class);
            List<OperationJd> operationJds = util.importExcel(multipartFile.getInputStream());

            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

            List<OperationJd> list = new ArrayList<>();
            for(OperationJd temp : operationJds){

                temp.setBaseId(IdUtil.simpleUUID());
                temp.setBaseCreateTime(DateUtils.getNowDate());
                temp.setBaseUpdateTime(DateUtils.getNowDate());
                temp.setBaseCreateBy(SecurityUtils.getUserId());
                temp.setBaseUpdateBy(SecurityUtils.getUserId());
                temp.setYears(operationJd.getYears());

                //设置当前登陆人的区县
                temp.setProvince(sysUser.getProvince());
                temp.setCity(sysUser.getCity());
                temp.setCounty(sysUser.getDistrict());
//                temp.setQuarter(DictUtils.getDictValue("kfq_khqj",operationJd.getQuarter()));
                temp.setQuarter(operationJd.getQuarter());
//                temp.setProvince(redisTemplate.opsForValue().get("areacode:" + temp.getProvince()));
//                temp.setCity(redisTemplate.opsForValue().get("areacode:" + temp.getCity()));
//                temp.setCounty(redisTemplate.opsForValue().get("areacode:" + temp.getCounty()));

                list.add(temp);
                if(list.size() > BTACH_COUNT){
                    operationJdFxMapper.batchInsert(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                operationJdFxMapper.batchInsert(list);
                list.clear();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 查询用地税收表格
     *
     * @param operationJd
     * @return
     */
    @Override
    public List<OperationJd> selectJdYdssTable(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);

        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }

        //查询城市数据最新的年份
        List<OperationJd> cityNewYear = operationJdFxMapper.selectCityDataMaxYear();
        Map<String,String> cityNewYearMap = new HashMap<>();
        if(cityNewYear == null || cityNewYear.isEmpty()){
            cityNewYear = new ArrayList<>();
        }else{
            cityNewYearMap = cityNewYear.stream().collect(Collectors.toMap(OperationJd::getCity,OperationJd::getYears));
        }

        Map<String, String> cityAreaMap = operationAreas.stream().filter(k -> !StringUtils.equals(k.getAreaCode(),"370000")).sorted(Comparator.comparing(OperationArea::getAreaCode))
                .collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode,(oldValue, newValue) -> oldValue, TreeMap::new));

        List<OperationJd> resultList = new ArrayList();

        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);

                if(tempOperation == null){
                    OperationJd tempResult = new OperationJd();
                    tempResult.setCity(key);
                    tempResult.setYdssBigDecimal("-");
                    resultList.add(tempResult);
                }else{
                    //取出该市的税收
                    String actualTax = tempOperation.getActualTax();
                    //取出该市最新的年份
                    String newYears = cityNewYearMap.get(key);
                    if(StringUtils.isNotBlank(newYears)){
                        //查询出该市的用地总面积
                        CityOptions citySumAreaMap = operationJdFxMapper.selectCitySumArea(newYears, entry.getValue());
                        String sumArea = citySumAreaMap.getSumArea();
                        OperationJd tempResult = new OperationJd();
                        if(StringUtils.isBlank(actualTax) || StringUtils.isBlank(sumArea)){
                            tempResult.setYdssBigDecimal("-");
                        }else{
                            BigDecimal actualTaxBigDecimal = new BigDecimal(actualTax);
                            BigDecimal sumAreaBigDecimal = new BigDecimal(sumArea);
                            tempResult.setCity(key);
                            if(actualTaxBigDecimal.compareTo(BigDecimal.ZERO) == 0 || sumAreaBigDecimal.compareTo(BigDecimal.ZERO) == 0){
                                tempResult.setYdssBigDecimal("-");
                            }else{
                                BigDecimal r = actualTaxBigDecimal.divide(sumAreaBigDecimal, 2, BigDecimal.ROUND_HALF_UP);
                                tempResult.setYdssBigDecimal(r.toString());
                            }
                        }
                        resultList.add(tempResult);
                    }else{
                        OperationJd tempResult = new OperationJd();
                        tempResult.setCity(key);
                        tempResult.setYdssBigDecimal("-");
                        resultList.add(tempResult);
                    }
                }
            }else{
                OperationJd tempResult = new OperationJd();
                tempResult.setCity(key);
                tempResult.setYdssBigDecimal("-");
                resultList.add(tempResult);
            }
        }
        return resultList;
    }

    /**
     * @param operationJd
     * @return
     */
    @Override
    public Map<String, Object> selectJdYdssCharts(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);

        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }

        //查询城市数据最新的年份
        List<OperationJd> cityNewYear = operationJdFxMapper.selectCityDataMaxYear();
        Map<String,String> cityNewYearMap = new HashMap<>();
        if(cityNewYear == null || cityNewYear.isEmpty()){
            cityNewYear = new ArrayList<>();
        }else{
            cityNewYearMap = cityNewYear.stream().collect(Collectors.toMap(OperationJd::getCity,OperationJd::getYears));
        }

        Map<String, String> cityAreaMap = operationAreas.stream().collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode));
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);
                //取出该市的税收
                String actualTax = tempOperation.getActualTax();
                //取出该市最新的年份
                String newYears = cityNewYearMap.get(key);
                if(StringUtils.isNotBlank(newYears)){
                    //去亩产查询出该市的用地总面积
                    CityOptions citySumAreaMap = operationJdFxMapper.selectCitySumArea(newYears, entry.getValue());
                    String sumArea = citySumAreaMap.getSumArea();
                    if(StringUtils.isBlank(actualTax) || StringUtils.isBlank(sumArea)){
                        continue;
                    }else{
                        BigDecimal actualTaxBigDecimal = new BigDecimal(actualTax);
                        BigDecimal sumAreaBigDecimal = new BigDecimal(sumArea);

                        if(actualTaxBigDecimal.compareTo(BigDecimal.ZERO) == 0 || sumAreaBigDecimal.compareTo(BigDecimal.ZERO) == 0){
                            continue;
                        }else{
                            xAxis.add(key);
                            BigDecimal r = actualTaxBigDecimal.divide(sumAreaBigDecimal, 2, BigDecimal.ROUND_HALF_UP);
                            yAxis.add(r.toString());
                        }
                    }
                }

            }
        }
        Map<String,Object> reuslt = new HashMap<>(16);
        reuslt.put("xAxis",xAxis);
        reuslt.put("yAxis",yAxis);
        return reuslt;
    }


    /**
     * 查询用单位用地销售图表
     *
     * @param operationJd
     * @return
     */
    @Override
    public Map<String, Object> selectJdYdysxsCharts(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);

        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }

        //查询城市数据最新的年份
        List<OperationJd> cityNewYear = operationJdFxMapper.selectCityDataMaxYear();
        Map<String,String> cityNewYearMap = new HashMap<>();
        if(cityNewYear == null || cityNewYear.isEmpty()){
            cityNewYear = new ArrayList<>();
        }else{
            cityNewYearMap = cityNewYear.stream().collect(Collectors.toMap(OperationJd::getCity,OperationJd::getYears));
        }

        Map<String, String> cityAreaMap = operationAreas.stream().collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode));
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);
                //取出该市的税收
                String mainIncome = tempOperation.getMainIncome();
                //取出该市最新的年份
                String newYears = cityNewYearMap.get(key);

                if(StringUtils.isNotBlank(newYears)){
                    //查询出该市的用地总面积
                    CityOptions citySumAreaMap = operationJdFxMapper.selectCitySumArea(newYears, entry.getValue());
                    String sumArea = citySumAreaMap.getSumArea();
                    if(StringUtils.isBlank(mainIncome) || StringUtils.isBlank(sumArea)){

                    }else{
                        BigDecimal mainIncomeBigDecimal = new BigDecimal(mainIncome);
                        BigDecimal sumAreaBigDecimal = new BigDecimal(sumArea);

                        if(mainIncomeBigDecimal.compareTo(BigDecimal.ZERO) == 0 || sumAreaBigDecimal.compareTo(BigDecimal.ZERO) == 0){
                            continue;
                        }else{
                            xAxis.add(key);
                            BigDecimal r = mainIncomeBigDecimal.divide(sumAreaBigDecimal, 2, BigDecimal.ROUND_HALF_UP);
                            yAxis.add(r.toString());
                        }
                    }
                }
            }
        }
        Map<String,Object> reuslt = new HashMap<>(16);
        reuslt.put("xAxis",xAxis);
        reuslt.put("yAxis",yAxis);
        return reuslt;
    }

    /**
     * 查询用单位用地销售表格
     *
     * @param operationJd
     * @return
     */
    @Override
    public List<OperationJd> selectJdYdysxsTable(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);

        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }

        //查询城市数据最新的年份
        List<OperationJd> cityNewYear = operationJdFxMapper.selectCityDataMaxYear();
        Map<String,String> cityNewYearMap = new HashMap<>();
        if(cityNewYear == null || cityNewYear.isEmpty()){
            cityNewYear = new ArrayList<>();
        }else{
            cityNewYearMap = cityNewYear.stream().collect(Collectors.toMap(OperationJd::getCity,OperationJd::getYears));
        }

        Map<String, String> cityAreaMap = operationAreas.stream().filter(k -> !StringUtils.equals(k.getAreaCode(),"370000")).sorted(Comparator.comparing(OperationArea::getAreaCode))
                .collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode,(oldValue, newValue) -> oldValue, TreeMap::new));

        List<OperationJd> resultList = new ArrayList();

        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);

                if(tempOperation == null){
                    OperationJd tempResult = new OperationJd();
                    tempResult.setCity(key);
                    tempResult.setYdssBigDecimal("-");
                    resultList.add(tempResult);
                }else{
                    //取出该市的税收
                    String mainIncome = tempOperation.getMainIncome();
                    //取出该市最新的年份
                    String newYears = cityNewYearMap.get(key);
                    if(StringUtils.isNotBlank(newYears)){
                        //查询出该市的用地总面积
                        CityOptions citySumAreaMap = operationJdFxMapper.selectCitySumArea(newYears, entry.getValue());
                        String sumArea = citySumAreaMap.getSumArea();
                        OperationJd tempResult = new OperationJd();
                        tempResult.setCity(key);
                        if(StringUtils.isBlank(mainIncome) || StringUtils.isBlank(sumArea)){
                            tempResult.setDwydxssrBigDecimal("-");
                        }else{
                            BigDecimal mainIncomeBigDecimal = new BigDecimal(mainIncome);
                            BigDecimal sumAreaBigDecimal = new BigDecimal(sumArea);

                            if(mainIncomeBigDecimal.compareTo(BigDecimal.ZERO) == 0 || sumAreaBigDecimal.compareTo(BigDecimal.ZERO) == 0){
                                tempResult.setDwydxssrBigDecimal("-");
                            }else{
                                BigDecimal r = mainIncomeBigDecimal.divide(sumAreaBigDecimal, 2, BigDecimal.ROUND_HALF_UP);
                                tempResult.setDwydxssrBigDecimal(r.toString());
                            }
                        }
                        resultList.add(tempResult);
                    }else{
                        OperationJd tempResult = new OperationJd();
                        tempResult.setCity(key);
                        tempResult.setDwydxssrBigDecimal("-");
                        resultList.add(tempResult);
                    }
                }
            }else{
                OperationJd tempResult = new OperationJd();
                tempResult.setCity(key);
                tempResult.setDwydxssrBigDecimal("-");
                resultList.add(tempResult);
            }
        }
        return resultList;
    }

    /**
     * 查询城市的税收
     *
     * @param operationJd
     * @return
     */
    @Override
    public List<OperationJd> queryCitySsTable(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);

        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String, String> cityAreaMap = operationAreas.stream().filter(k -> !StringUtils.equals(k.getAreaCode(),"370000")).sorted(Comparator.comparing(OperationArea::getAreaCode))
                .collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode,(oldValue, newValue) -> oldValue, TreeMap::new));

        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }
        List<OperationJd> resultList = new ArrayList();
        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);
                //取出该市的税收
                String actualTax = tempOperation.getActualTax();
                OperationJd tempResult = new OperationJd();
                tempResult.setCity(key);
                tempResult.setActualTax(actualTax);
                resultList.add(tempResult);
            }else{
                OperationJd tempResult = new OperationJd();
                tempResult.setCity(key);
                tempResult.setActualTax("-");
                resultList.add(tempResult);
            }
        }
        return resultList;
    }

    /**
     * 查询城市的主要销售收
     *
     * @param operationJd
     * @return
     */
    @Override
    public List<OperationJd> queryCityMiTable(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);

        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String, String> cityAreaMap = operationAreas.stream().filter(k -> !StringUtils.equals(k.getAreaCode(),"370000")).sorted(Comparator.comparing(OperationArea::getAreaCode))
                .collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode,(oldValue, newValue) -> oldValue, TreeMap::new));

        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }
        List<OperationJd> resultList = new ArrayList();
        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);
                //取出该市的税收
                String mainIncome = tempOperation.getMainIncome();
                OperationJd tempResult = new OperationJd();
                tempResult.setCity(key);
                tempResult.setMainIncome(mainIncome);
                resultList.add(tempResult);
            }else{
                OperationJd tempResult = new OperationJd();
                tempResult.setCity(key);
                tempResult.setMainIncome("-");
                resultList.add(tempResult);
            }
        }
        return resultList;
    }

    /**
     * 查询城市的主要销售收入表格
     *
     * @param operationJd
     * @return
     */
    @Override
    public Map<String,Object> queryCityMiCharts(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);
        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String, String> cityAreaMap = operationAreas.stream().filter(k -> !StringUtils.equals(k.getAreaCode(),"370000")).sorted(Comparator.comparing(OperationArea::getAreaCode))
                .collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode,(oldValue, newValue) -> oldValue, TreeMap::new));

        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        List<OperationJd> resultList = new ArrayList();
        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);
                //取出该市的税收
                String mainIncome = tempOperation.getMainIncome();
                xAxis.add(key);
                yAxis.add(mainIncome);
            }
        }
        Map<String,Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }

    /**
     * 查询城市的税收图表
     *
     * @param operationJd
     * @return
     */
    @Override
    public Map<String, Object> queryCitySsCharts(OperationJd operationJd) {
        List<OperationJd> operationJds = operationJdFxMapper.selectJdBaseInfo(operationJd);
        List<OperationArea> operationAreas = operationJdFxMapper.selectCityInfo(operationJd);
        if(operationAreas == null || operationAreas.isEmpty()){
            throw new RuntimeException("未查询到16地市的信息");
        }
        Map<String, String> cityAreaMap = operationAreas.stream().filter(k -> !StringUtils.equals(k.getAreaCode(),"370000")).sorted(Comparator.comparing(OperationArea::getAreaCode))
                .collect(Collectors.toMap(OperationArea::getBaseName, OperationArea::getAreaCode,(oldValue, newValue) -> oldValue, TreeMap::new));

        Map<String,OperationJd> operationJdMap = new HashMap<>();
        if(operationJds == null || operationJds.isEmpty()){
            operationJds = new ArrayList<>();
        }else{
            operationJdMap = operationJds.stream().collect(Collectors.toMap(OperationJd::getCity,v->v));
        }
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        for(Map.Entry<String, String> entry : cityAreaMap.entrySet()){
            String key = entry.getKey();
            if(operationJdMap.containsKey(key)){
                OperationJd tempOperation = operationJdMap.get(key);
                //取出该市的税收
                String actualTax = tempOperation.getActualTax();
                xAxis.add(key);
                yAxis.add(actualTax);
            }
        }
        Map<String,Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }
}
